/*
 * PanelEditorPaths.java
 *
 * Created on December 8, 2007, 11:36 AM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.UI;

import hextd.hex.Hex;
import hextd.core.ContextNormal;
import hextd.wave.Path;
import hextd.wave.PathNormal;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author  hylke
 */
public class PanelEditorPaths extends javax.swing.JPanel {

	private DefaultListModel modelPathSelector;
	private DefaultListModel modelPathContents;
	private boolean stepInsert = true;
	private ContextNormal context;
	private boolean loading = false;

	/** Creates new form PanelEditorPaths */
	public PanelEditorPaths() {
	}

	public void setContext(ContextNormal c) {
		context = c;
		initComponents();
		setVisible(false);
		modelPathSelector = (DefaultListModel) jList_pathSelector.getModel();
		modelPathContents = (DefaultListModel) jList_pathContent.getModel();
	}

	public void resetPaths() {
		if (modelPathSelector != null) {
			modelPathSelector.clear();
			modelPathContents.clear();
			ArrayList<Path> paths = context.getPaths();
			for (int i = 0; i < paths.size(); i++) {
				modelPathSelector.add(i, "Path " + (i + 1) + " (" + paths.get(i).length() + ")");
			}
			invalidate();
		}
	}

	public void selectPath(int s) {
		loading = true;
		ArrayList<Path> paths = context.getPaths();
		for (int i = 0; i < paths.size(); i++) {
			paths.get(i).setSelected(false);
		}
		if (s >= 0 && s < paths.size()) {
			modelPathContents.clear();
			Path tempPath = paths.get(s);
			tempPath.setSelected(true);
			for (int i = 0; i < tempPath.length(); i++) {
				int[] tempStep = tempPath.getStepHexCoords(i);
				String step = "" + tempStep[0] + "," + tempStep[1];
				modelPathContents.add(i, step);
			}
			jSpinnerDelay.setValue(tempPath.getBaseDelay());
			jSpinnerOwner.setValue(tempPath.getOwner());
			jSpinnerTarget.setValue(tempPath.getTargetUser());
		}
		invalidate();
		loading = false;
	}

	private void addPath() {
		ArrayList<Path> paths = context.getPaths();
		PathNormal newPath = new PathNormal(0, 0, -1);
		newPath.setContext(context);
		newPath.finalise();
		newPath.resetScale();
		paths.add(newPath);
		resetPaths();
		selectPath(paths.size() - 1);
		if (context.loadedGameMap.paths == null) {
			context.loadedGameMap.paths = new ArrayList<PathNormal>();
		}
		context.loadedGameMap.paths.add(newPath);
	}

	private void readSpinners() {
		if (!loading) {
			ArrayList<Path> paths = context.getPaths();
			int pathNr = jList_pathSelector.getSelectedIndex();
			if (pathNr >= 0 && pathNr < paths.size()) {
				PathNormal p = (PathNormal) paths.get(pathNr);
				try {
					p.setBaseDelay(new Float(jSpinnerDelay.getValue().toString()).floatValue());
					p.setOwner((Integer) jSpinnerOwner.getValue());
					p.setTargetUser((Integer) jSpinnerTarget.getValue());
				} catch (NumberFormatException ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	private void removePath() {
		ArrayList<Path> paths = context.getPaths();
		int pathNr = jList_pathSelector.getSelectedIndex();
		if (pathNr >= 0 && pathNr < paths.size()) {
			Path remove = paths.remove(pathNr);
			if (remove instanceof PathNormal) {
				context.loadedGameMap.paths.remove((PathNormal) remove);
			}
			resetPaths();
		}
	}

	public void hexClicked(Hex hex) {
		if (isVisible()) {
			int pathNr = jList_pathSelector.getSelectedIndex();
			int stepNr = jList_pathContent.getSelectedIndex();
			ArrayList<Path> paths = context.getPaths();

			if (pathNr >= 0 && pathNr < paths.size() && stepNr <= paths.get(pathNr).length()) {
				int[] l = hex.getLocation();
				PathNormal p = (PathNormal) paths.get(pathNr);
				if (stepInsert) {
					p.insertStep(stepNr + 1, l[0], l[1]);
					stepNr++;
				} else {
					if (stepNr >= 0) {
						p.updateStep(stepNr, l[0], l[1]);
					}
				}
				selectPath(pathNr);
				jList_pathContent.setSelectedIndex(stepNr);
			}
		}
	}

	private void removeStep() {
		ArrayList<Path> paths = context.getPaths();
		int pathNr = jList_pathSelector.getSelectedIndex();
		int stepNr = jList_pathContent.getSelectedIndex();
		if (pathNr >= 0 && pathNr < paths.size() && stepNr >= 0 && stepNr < paths.get(pathNr).length()) {
			PathNormal p = (PathNormal) paths.get(pathNr);
			p.removeStep(stepNr);
			selectPath(pathNr);
			jList_pathContent.setSelectedIndex(stepNr - 1);
		}
	}

	private void selectStep() {
		ArrayList<Path> paths = context.getPaths();
		int pathNr = jList_pathSelector.getSelectedIndex();
		int stepNr = jList_pathContent.getSelectedIndex();
		if (pathNr >= 0 && pathNr < paths.size() && stepNr >= 0 && stepNr <= paths.get(pathNr).length()) {
			int[] tempStep = paths.get(pathNr).getStepHexCoords(stepNr);
			Hex h = context.getGameGrid().getHex(tempStep[0], tempStep[1]);
			context.highlightHex(h);
		}
	}

	@Override
	public void setVisible(boolean aFlag) {
		if (aFlag) {
			resetPaths();
		}
		super.setVisible(aFlag);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jButton_pathMin = new javax.swing.JButton();
        jButton_pathPlus = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList_pathSelector = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList_pathContent = new javax.swing.JList();
        jButton_contentMin = new javax.swing.JButton();
        jToggleButton_contentMode = new javax.swing.JToggleButton();
        jSpinnerDelay = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSpinnerOwner = new javax.swing.JSpinner();
        jSpinnerTarget = new javax.swing.JSpinner();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Paths"));
        setMaximumSize(new java.awt.Dimension(200, 2147483647));
        setMinimumSize(new java.awt.Dimension(200, 63));
        setPreferredSize(new java.awt.Dimension(200, 289));
        setLayout(new java.awt.GridBagLayout());

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setResizeWeight(0.3);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jButton_pathMin.setText("-");
        jButton_pathMin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pathMinActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 2);
        jPanel1.add(jButton_pathMin, gridBagConstraints);

        jButton_pathPlus.setText("+");
        jButton_pathPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pathPlusActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
        jPanel1.add(jButton_pathPlus, gridBagConstraints);

        jList_pathSelector.setModel(new DefaultListModel());
        jList_pathSelector.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList_pathSelector.setVisibleRowCount(3);
        jList_pathSelector.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_pathSelectorValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList_pathSelector);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel1.add(jScrollPane2, gridBagConstraints);

        jSplitPane1.setTopComponent(jPanel1);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jList_pathContent.setModel(new DefaultListModel());
        jList_pathContent.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList_pathContent.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList_pathContentValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(jList_pathContent);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 0.03;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        jPanel2.add(jScrollPane3, gridBagConstraints);

        jButton_contentMin.setText("-");
        jButton_contentMin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_contentMinActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        jPanel2.add(jButton_contentMin, gridBagConstraints);

        jToggleButton_contentMode.setText("Insert");
        jToggleButton_contentMode.setContentAreaFilled(false);
        jToggleButton_contentMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton_contentModeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.01;
        jPanel2.add(jToggleButton_contentMode, gridBagConstraints);

        jSpinnerDelay.setModel(new SpinnerNumberModel(0,0,20,0.1));
        jSpinnerDelay.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinnerDelayStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jSpinnerDelay, gridBagConstraints);

        jLabel1.setText("Delay:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Owner:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jLabel2, gridBagConstraints);

        jLabel3.setText("Target:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jLabel3, gridBagConstraints);

        jSpinnerOwner.setModel(new javax.swing.SpinnerNumberModel(-1, -1, 12, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jSpinnerOwner, gridBagConstraints);

        jSpinnerTarget.setModel(new javax.swing.SpinnerNumberModel(0, 0, 12, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jSpinnerTarget, gridBagConstraints);

        jSplitPane1.setRightComponent(jPanel2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0E-4;
        gridBagConstraints.weighty = 0.05;
        add(jSplitPane1, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void jSpinnerDelayStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinnerDelayStateChanged
		readSpinners();
    }//GEN-LAST:event_jSpinnerDelayStateChanged

    private void jButton_pathMinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pathMinActionPerformed
		removePath();
    }//GEN-LAST:event_jButton_pathMinActionPerformed

    private void jButton_pathPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pathPlusActionPerformed
		addPath();
    }//GEN-LAST:event_jButton_pathPlusActionPerformed

    private void jList_pathContentValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_pathContentValueChanged
		selectStep();
    }//GEN-LAST:event_jList_pathContentValueChanged

    private void jButton_contentMinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_contentMinActionPerformed
		removeStep();
    }//GEN-LAST:event_jButton_contentMinActionPerformed

    private void jToggleButton_contentModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton_contentModeActionPerformed
		stepInsert = !jToggleButton_contentMode.isSelected();
		if (stepInsert) {
			jToggleButton_contentMode.setText("Insert");
		} else {
			jToggleButton_contentMode.setText("Move");
		}
    }//GEN-LAST:event_jToggleButton_contentModeActionPerformed

    private void jList_pathSelectorValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList_pathSelectorValueChanged
		selectPath(jList_pathSelector.getSelectedIndex());
    }//GEN-LAST:event_jList_pathSelectorValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_contentMin;
    private javax.swing.JButton jButton_pathMin;
    private javax.swing.JButton jButton_pathPlus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList_pathContent;
    private javax.swing.JList jList_pathSelector;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSpinner jSpinnerDelay;
    private javax.swing.JSpinner jSpinnerOwner;
    private javax.swing.JSpinner jSpinnerTarget;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToggleButton jToggleButton_contentMode;
    // End of variables declaration//GEN-END:variables
}
